package com.tianruan.dc.modules.repo.controller;
import com.tianruan.dc.common.api.ApiRest;
import com.tianruan.dc.common.api.controller.BaseController;
import com.tianruan.dc.common.api.dto.BaseIdReqDTO;
import com.tianruan.dc.common.api.dto.BaseIdsReqDTO;
import com.tianruan.dc.common.utils.response.VResponse;
import com.tianruan.dc.modules.repo.dto.RepoDTO;
import com.tianruan.dc.modules.repo.dto.response.RepoRespDTO;
import com.tianruan.dc.modules.repo.entity.Repo;
import com.tianruan.dc.modules.repo.service.RepoService;
import com.tianruan.dc.modules.sys.user.entity.Result;
import com.tianruan.dc.modules.sys.user.utils.PageBean;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
* <p>
* 题库控制器
* </p>
*/
@RestController
@RequestMapping("/api/repo")
public class RepoController extends BaseController {

    @Autowired
    private RepoService baseService;

//    @Autowired
//    private QuRepoService quRepoService;

    /**
    * 题库添加或修改
    * @param reqDTO
    * @return
    */
    @PostMapping("/save")
    //授权
    @PreAuthorize("hasAnyAuthority('sys:super:admin','sys:teacher')")
    public Result save(@RequestBody RepoDTO reqDTO) {
        baseService.save(reqDTO);
        return Result.ok("保存题库成功！");
    }

    /**
    * 批量删除
    * @param reqDTO
    * @return
    */
    @PostMapping("/delete")
    //授权
    @PreAuthorize("hasAnyAuthority('sys:super:admin','sys:teacher')")
    public Result edit(@RequestBody BaseIdsReqDTO reqDTO) {
        //根据ID删除
        baseService.removeByIds(reqDTO.getIds());
        return Result.ok("批量删除题库成功！");
    }

    /**
    * 查找详情
    * @param reqDTO
    * @return
    */
    @PostMapping( "/detail")
    //授权
    @PreAuthorize("hasAnyAuthority('sys:super:admin','sys:teacher')")
    public ApiRest<RepoDTO> find(@RequestBody BaseIdReqDTO reqDTO) {
        Repo entity = baseService.getById(reqDTO.getId());
        RepoDTO dto = new RepoDTO();
        BeanUtils.copyProperties(entity, dto);
        return super.success(dto);
    }

    /**
    * 分页查找
    */

    @PostMapping("/pagRepo")
    //授权
    @PreAuthorize("hasAnyAuthority('sys:super:admin','sys:teacher')")
    public VResponse<PageBean<RepoRespDTO>> pageRepo(
            @RequestParam(defaultValue = "1")  Integer page,
            @RequestParam(defaultValue = "10")  Integer pageSize,
            @RequestParam(value = "title",required = false) String title
    )
    {
        PageBean<RepoRespDTO> RepoRespDTO=baseService.PageRepo(page,pageSize,title);
        return VResponse.success(RepoRespDTO);
    }

}
